package com.gang.service;

import com.gang.mapper.UserMapper;
import com.gang.pojo.User;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User queryUserByUsername(String username) {

        return userMapper.queryUserByUsername(username);

    }

    @Override
    public int insertUser(User user) {
        if (userMapper.queryUserByUsername(user.getUsername()) == null) {
            String hashpw = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
            user.setPassword(hashpw);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setUserLevel(1);
            user.setStatus(1);
            return userMapper.insertUser(user);
        }else{
            return 0;
        }
    }

    @Override
    public List<User> queryAllUser() {
        return userMapper.queryAllUser();
    }

    @Override
    public String querryUserDid(String username) {
      return userMapper.querryUserDid(username);
    }

    @Override
    public int addDid(String did, String username) {
        String s = userMapper.querryUserDid(username);
        if(s==null){
            return userMapper.addDid(did, username);
        }else
            return -1;
    }

    @Override
    public int addaddress(String username, String address) {
            return userMapper.addaddress(username, address);
    }
}
